package com.mijie.homi.api;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mijie.core.Status;
import com.mijie.homi.common.constant.HomiStatusCode;
import com.mijie.homi.common.domain.user.Privacy;
import com.mijie.homi.service.user.PrivacyService;
import com.mijie.homi.util.ServerHelper;
import com.mijie.util.IOUtil;

@Controller
@RequestMapping("/privacy")
public class PrivacyApi {

	private static Logger logger = Logger.getLogger(UserApi.class);
	
	@Autowired
	private PrivacyService privacyService;
	
	@RequestMapping(value="/apply",method=RequestMethod.POST)
	public @ResponseBody void apply(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String content = IOUtil.getContent4InputStream(request.getInputStream());
		JSONObject json = JSONObject.fromObject(content);
		Integer userId = ServerHelper.getUserId(request);
		Status status = new Status(request,response);
		if(json.containsKey("master") && json.containsKey("module"))
			privacyService.privacyApply(userId, json.getInt("master"), json.getString("module"));
		else
			status.setCode(HomiStatusCode.PARAM_ABSENT);
		
		status.write();
	}
	
	@RequestMapping(value="/apply/accept",method=RequestMethod.POST)
	public @ResponseBody void applyHandle(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String content = IOUtil.getContent4InputStream(request.getInputStream());
		JSONObject json = JSONObject.fromObject(content);
		Integer userId = ServerHelper.getUserId(request);
		Status status = new Status(request,response);
		if(json.containsKey("applicant") && json.containsKey("module")){
			privacyService.acceptPrivacyApply(json.getInt("applicant"),userId, json.getString("module"));
		}else{
			status.setCode(HomiStatusCode.PARAM_ABSENT);
		}
		status.write();
	}
	
	@RequestMapping(value="/apply/reject",method=RequestMethod.POST)
	public @ResponseBody void rejectHandle(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String content = IOUtil.getContent4InputStream(request.getInputStream());
		JSONObject json = JSONObject.fromObject(content);
		Integer userId = ServerHelper.getUserId(request);
		Status status = new Status(request,response);
		if(json.containsKey("applicant") && json.containsKey("module"))
			privacyService.rejectPrivacyApply(json.getInt("applicant"),userId, json.getString("module"));
		else
			status.setCode(HomiStatusCode.PARAM_ABSENT);
		status.write();
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public @ResponseBody void getSetting(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Integer userId = ServerHelper.getUserId(request);
		Privacy privacy = privacyService.getPrivacy(userId);
		JSONObject result = JSONObject.fromObject(privacy);
		Status status = new Status(request,response);
		status.setResult(result);
		status.write();
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody void updatePrivacySetting(HttpServletRequest request,HttpServletResponse response){
		String content = null;
		try {
			content = IOUtil.getContent4InputStream(request.getInputStream());
			JSONObject json = JSONObject.fromObject(content);
			Integer userId = ServerHelper.getUserId(request);
			Privacy privacy = (Privacy) JSONObject.toBean(json, Privacy.class);
			privacy.setId(userId);
			int rows = privacyService.updatePrivacy(privacy);
			Status status = new Status(request,response);
			if(rows==0) status.setCode(HomiStatusCode.API_EXCEPTION);
			status.write();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
}
